import static org.junit.Assert.*;

import android.app.Activity;
import android.cs2340UDT.*;
import android.test.ActivityInstrumentationTestCase2;
import android.widget.EditText;

import com.jayway.android.robotium.solo.Solo;

public class UI_Login_Test extends ActivityInstrumentationTestCase2<CS2340AndroidUDTActivity>{
	
	private CS2340AndroidUDTActivity activity;
	
	private Solo tester;
	
	EditText nameInputField;

	public UI_Login_Test(String testPackage, Class<CS2340AndroidUDTActivity> testClass) {
		super(testPackage, testClass);
		// TODO Auto-generated constructor stub
	}
	
	protected void setUp() throws Exception {
		super.setUp();
		
		//setActivityInitialTouchMode(false);
		
		activity = super.getActivity();
		
		tester = new Solo(getInstrumentation(), getActivity());
		
		//nameInputField = (EditText) activity.findViewById(R.id.username);
		
	}

	@Test
	public void testFilterByCompletion() {
		// Get through log in - use empty user parameters
		tester.clickOnText("Register New Account");
		tester.clickOnText("Create Account");
		tester.clickOnText("Login");
		
		// Create a new category
		tester.clickOnImageButton(2); //pressing the add button
		tester.getEditText(0).setText("Test Category");
		tester.clickOnText("Create New List");
		
		//enter category and create two items
		
		tester.clickOnText("Test Category"); // enter category
		
		tester.clickOnImageButton(2); //pressing the add button
		tester.getEditText(0).setText("Test Item"); // enter task name
		tester.getEditText(1).setText("Atlanta, Ga"); // enter location
		tester.getEditText(2).setText("12"); // enter month
		tester.getEditText(3).setText("25"); // enter day
		tester.getEditText(4).setText("2012"); // enter year
		tester.getEditText(0).setText("This is a test"); // enter description
		tester.clickOnText("Create Task"); // finish creating first item
		
		tester.clickOnImageButton(2); //pressing the add button
		tester.getEditText(0).setText("Test Item 2"); // enter task name
		tester.getEditText(1).setText("Atlanta, Ga"); // enter location
		tester.getEditText(2).setText("12"); // enter month
		tester.getEditText(3).setText("25"); // enter day
		tester.getEditText(4).setText("2012"); // enter year
		tester.getEditText(0).setText("This is a test"); // enter description
		tester.clickOnText("Create Task"); // finish creating first item
		
		//enter test item 2 and mark as complete
		tester.clickOnText("Test Item 2"); // enter test item 2
		tester.clickOnButton(0); // mark the item complete
		tester.clickOnImageButton(0); // press the back button
		
		// enter settings and filter by "completed"
		tester.clickOnImageButton(1); // press settings button
		tester.pressSpinnerItem(0,0); // mark filtering by "completed"
		// activity automatically goes back to the list of items
		
		//store whether item one is in the displayed list
		boolean testItemOne = tester.searchText("Test Item", 1);
		//store whether item two is NOT in the displayed list
		boolean testItemTwo = tester.searchText("Test Item 2", 0);
		
		//test if the filter worked correctly
		assertEquals(true, testItemOne);
		assertEquals(true, testItemTwo);
		
		
	}
	
	public void testLoginUi() {
		// Create Account
		tester.clickOnText("Register New Account");
		tester.getEditText(0).setText("John Doe"); // Test User Name Input
		tester.getEditText(1).setText("Test User"); // Test username input
		tester.getEditText(5).setText("Correct Password"); // test user password
		tester.getEditText(6).setText("Correct Password"); // test user password confirmation
		tester.clickOnText("Create Account");
		
		// attempt to login with incorrect user name and incorrect password
		tester.getEditText(0).setText("Some Random User");
		tester.getEditText(1).setText("Some Random Password");
		tester.clickOnText("Login");
		
		// confirm that login failed (still on login screen)
		boolean onLoginScreen = tester.searchText("Login");
		assertEquals(true, onLoginScreen);
		
		// attempt to login with correct user name but wrong password
		tester.getEditText(0).setText("Test User");
		tester.getEditText(1).setText("Incorrect Password");
		tester.clickOnText("Login");
		
		// confirm that login failed (still on login screen)
		onLoginScreen = tester.searchText("Login");
		assertEquals(true, onLoginScreen);
		
		// attempt to login with incorrect user name but a password valid for another user
		tester.getEditText(0).setText("Some Random User");
		tester.getEditText(1).setText("Correct Password");
		tester.clickOnText("Login");
		
		// confirm that login failed (still on login screen)
		onLoginScreen = tester.searchText("Login");
		assertEquals(true, onLoginScreen);
		
		// attempt to login with valid user name and password combination
		tester.getEditText(0).setText("Test User");
		tester.getEditText(1).setText("Correct Password");
		tester.clickOnText("Login");
		
		// confirm that login was successful
		boolean onMainScreen = tester.searchText("Lists");
		assertEquals(true, onMainScreen);
		
	}

}
